package org.iabako.server.controller.timer.business;

import org.apache.log4j.Logger;
import org.iabako.server.dao.business.ClientDAO;
import org.iabako.server.dao.business.SaleDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by jose on 17/11/14.
 */
@Controller
public class ClientSaleStatusControllerTimer {

    static Logger log = Logger.getLogger(ClientSaleStatusControllerTimer.class.getName());

    @Autowired
    private ClientDAO clientDAO;
    @Autowired
    private SaleDAO saleDAO;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Scheduled(initialDelay = 10000, fixedRate = 3600000)
    public void action() throws Exception {
        log.info("Executing ClientSaleStatusControllerTimer...");

        try {
            int result = saleDAO.updateStatus();
            log.info("Number of Sale STATUS updated : " + result);

            result = clientDAO.updateStatus();
            log.info("Number of Client STATUS updated : " + result);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }
}
